/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	import com.edi.runtime.Event.*;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Tree;
	import mx.controls.treeClasses.DefaultDataDescriptor;
	import mx.events.ListEvent;
	
	/**
	 * 继承自Tree，并实现了IEDIDataLoad 借口  
	 * 
	 */	
	public class EDITree extends Tree implements IEDIDataLoad
	{
		private var _dataSourceName:String;
		/**
		 * 与pidField有关的字段
		 */		
		public var idField:String;
		/**
		 * 父字段 
		 */		
		public var pidField:String;
		/**
		 * 数据字段 
		 */		
		public var dataField:String;
		/**
		 * 根值 
		 */		
		public var rootValue:String;
		/**
		 * 用于排序的字段 
		 */		
		public var orderByField:String;
		/**
		 * 排序方式 
		 */		
		public var desc:Boolean=false;
		private var datapc:DefaultDataDescriptor
		private var treeXML:XML=XML(<xml />);
		
		/**
		 *  构造EDITree组件
		 * 
		 */		
		public function EDITree()
		{
			super();
			datapc=new DefaultDataDescriptor();
			this.addEventListener(ListEvent.CHANGE,changeHandler);
		}
		
		/**
		 * 数据源名称 
		 * @param value
		 * 
		 */		
		public function set dataSourceName(value:String):void
		{
			_dataSourceName=value;
			EDIEventProxy.listenerDataLoad(value,this);
		}
		
		
		public function get dataSourceName():String{
			return _dataSourceName;
		}
		
		/**
		 *  实现IEDIDataLoad接口的属性，数据加载事件
		 * @param event
		 * 
		 */		
		public function dataLoadHandler(event:EDIDataEvent):void
		{
			dataProvider=(event.value as EDIDataTable).toTree(idField,pidField,rootValue,dataField,orderByField,desc);
		}
		
		override public function set dataProvider(value:Object):void
		{
			if(value is XMLList)
			{
				treeXML=XML(<xml />);
				treeXML.appendChild(value)
			}
			super.dataProvider=value;
			expand();
			EDIApplication.outputDebug("EDITree-->value: "+value);
		}
		
		/**
		 * 返回有二维表格转换生成的树型xml 
		 * @return 
		 * 
		 */		
		public function get dataTree():XML
		{
			return treeXML;
		}
		
		/**
		 * 删除该节点 
		 * 
		 */		
		public function removeItem():void
		{
			if(selectedItem!=null)
			{
				if(this.dataProvider is EDIDataTree)
				{
					(this.dataProvider as EDIDataTree).removeItem(selectedItem);
				}
				else if(this.dataProvider is XMLListCollection)
				{
					datapc.removeChildAt(selectedItem.parent(),selectedItem,(selectedItem as XML).childIndex());
				}
			}
		}
		
		/**
		 * 返回当前DataTable行 
		 * @return 
		 * 
		 */		
		public function get selectedDataTreeItem():Object
		{
			var obj:Object=super.selectedItem;
			if(this.dataProvider is EDIDataTree)
			{
				obj=(this.dataProvider as EDIDataTree).getDataTableItem(obj);
			}
			return obj;			
		}
		
		/**
		 * 返回当前DataTable的索引 
		 * @return 
		 * 
		 */		
		public function get selectDataTreeIndex():int
		{
			var index:int=-1;
			if(this.dataProvider is EDIDataTree)
			{
				index=(this.dataProvider as EDIDataTree).getDataTableIndex(selectedItem);
			}
			return index
		}
		
		/**
		 * 显示当前节点的QPath表 
		 * @return 
		 * 
		 */		
		public function get selectedQItem():Object
		{
			var ret:Object=selectedItem;
			if(this.dataProvider is EDIDataTree)
			{
				ret=(this.dataProvider as EDIDataTree).treeNodeToQItem(ret);
			}
			return ret;
		}
		
		
		/**
		 * 将QPath标加入到关联的数据库中 此时DataTree的_dataLink必须为真才能影响 
		 * @param item
		 * 
		 */		
		public function addQItem(item:Object):void
		{
			if(dataProvider is  EDIDataTree)
			{
				item=(dataProvider as  EDIDataTree).addQItem(item);
				if(item[0]!=null)
				{
					this.expandChildrenOf(item[0],true);
				}
			}
			else
			{
				
				var tempXML:XML=treeXML;
				var selectIndex:int=0;
				if(selectedItem!=null){
					tempXML=selectedItem as XML;
					selectIndex=selectedIndex;
				}
				tempXML.appendChild(item);
				super.dataProvider=treeXML.children();
				expand(selectIndex);
			}
		}
		
		/**
		 * 节点变化时间 
		 * @param event
		 * 
		 */		
		public function changeHandler(event:ListEvent):void
		{
			if(this.dataProvider is EDIDataTree)
			{
				(this.dataProvider as EDIDataTree).getDataTableIndex(selectedItem);
			}
		}
		
		/**
		 * 展开索引值下面的树 
		 * @param selectIndex
		 * 
		 */		
		public function expand(selectIndex:int=-1):void
		{
			validateNow();
			if(selectIndex==-1)
			{
				if(this.selectedItem!=null)
					selectIndex=selectedIndex;
				else
					selectedIndex=0;
			}
			else
			{	
				selectedIndex=selectIndex;
			}
			
			expandChildrenOf(selectedItem,true);
			selectedIndex=selectIndex;
		}
		
//		private function order():void
//		{
//			var xmlList:XMLList=XMLList(this.dataProvider);
//			var iCount:int=xmlList.length();
//			if(iCount!=0)
//			{
//				for(var i:int=0;i<iCount;i++)
//				{
//					orderSub(XML(xmlList[i]));
//				}
//				xmlList=XMLUnit.sortXML(xmlList,orderByField,desc);
//			}
//		}
//		
//		private function orderSub(value:XML):void
//		{
//			var xmlList:XMLList=value.children();
//			var iCount:int=xmlList.length();
//			if(iCount!=0)
//			{
//				for(var i:int=0;i<iCount;i++)
//				{
//					orderSub(XML(xmlList[i]));
//				}
//				xmlList=XMLUnit.sortXML(xmlList,orderByField,desc);
//			}
//		}
		
	}
}